package com.song.ssm.month.web;

import com.song.ssm.month.pojo.User;
import com.song.ssm.month.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/9/25
 * @Version 1.0
 **/
@Controller
public class UserController {
     @Resource
     private IUserService userService;

     @GetMapping("/list")
     public String list(Model model,
                        @RequestParam(name = "offset",defaultValue = "1")Integer offset,
                        @RequestParam(name = "pagesize",defaultValue = "3") Integer pagesize,
                        @RequestParam(name="user",required = false)User user){
          model.addAttribute("pager",this.userService.page(offset,pagesize,user));
          return "list";
     }
//     public @ResponseBody List<User> findAll(){
//         return this.userService.findUsers();
//     }

     @GetMapping("/login")
     public String tologin(){
         return  "login";
     }

     @PostMapping("/login")
     public String login(User user, HttpSession session, Model model){
         User login = this.userService.login(user);
         if(login!=null){
             session.setAttribute("logUser",login);
             //
             return "redirect:/list";
         }else{
             model.addAttribute("mess","<font color='red'>登录失败！</font>");
             return "login";

         }
     }

     @GetMapping("/logout")
     public String logout(HttpSession session){
          if(session.getAttribute("logUser")!=null){
              session.removeAttribute("logUser");
          }
          return "redirect:login";
     }

     @RequestMapping(value = "/add",method = RequestMethod.GET)
     public String toadd(Model model){
         model.addAttribute("roles",this.userService.roles());
         return "add";
     }

     @RequestMapping(value = "/add",method = RequestMethod.POST)
     public String add( User user,Model model){
           if(this.userService.save(user)){
               return "redirect:list";
           }else{
               model.addAttribute("msg","保存失败！");
               return "add";
           }
     }

     @GetMapping("/hasUsername/{username}")
     public @ResponseBody boolean hasUsername(@PathVariable String username){
            return this.userService.hashName(username);
     }

     @GetMapping("/update")
     public String find(int uid,Model model){
         //获取所有的角色集合
         model.addAttribute("roles",this.userService.roles());
         //要修改的用户对象
         model.addAttribute(this.userService.findById(uid));
         return "update";
     }

     @PostMapping("/update")
     public String update(User user){
          this.userService.save(user);
          return "redirect:/list";

     }


     @GetMapping("/status/{id}")
     public @ResponseBody boolean status(@PathVariable int id){
          return this.userService.status(id);
     }


    /**
     * 返回要修改的对象的角色集合（rid）
     * @param uid
     * @return
     */
     @GetMapping("/findRoles")
     public @ResponseBody List<Integer> findRoles(int uid){
         return this.userService.findRoles(uid);
     }

     @GetMapping("/del")
     public @ResponseBody boolean del(String ids){
         return  this.userService.del(ids);
     }
}
